//
//  LoginViewController.m
//  F50
//
//  Created by 郝 巍 on 15/5/5.
//  Copyright (c) 2015年 ZACK. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "DealViewController.h"

@interface LoginViewController ()<UITextFieldDelegate>

@end

@implementation LoginViewController

- (void)createBackground
{
    self.navigationController.navigationBarHidden = YES;
    UIImageView *bgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    bgView.image = [UIImage imageNamed:@"background"];
    bgView.userInteractionEnabled = YES;
    [self.view addSubview:bgView];
    
    
    //中间标题
    UILabel *  nav_title=[[UILabel alloc] initWithFrame:CGRectMake(WIDTH/2 - 80, 20, 160, 40)];
    nav_title.text=@"登录";
    nav_title.textAlignment = NSTextAlignmentCenter;
    nav_title.font=[UIFont boldSystemFontOfSize:20];
    nav_title.textColor=[UIColor whiteColor];
    nav_title.backgroundColor=[UIColor clearColor];
    [bgView addSubview:nav_title];
    
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(15, 80, WIDTH - 30, 100)];
    view.layer.cornerRadius = 8;
    view.backgroundColor = [UIColor whiteColor];
    view.userInteractionEnabled = YES;
    [bgView addSubview:view];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, WIDTH - 30, 1)];
    label.backgroundColor = RGB(204, 204, 210);
    [view addSubview:label];
    
    UILabel *phoneLab = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 50, 30)];
    phoneLab.text = @"账号";
    [view addSubview:phoneLab];
    
    UILabel *passLab = [[UILabel alloc]initWithFrame:CGRectMake(10, 60, 50, 30)];
    passLab.text = @"密码";
    [view addSubview:passLab];
    
    self.phoneNum = [[UITextField alloc]initWithFrame:CGRectMake(60, 10, WIDTH - 90, 30)];
    self.phoneNum.placeholder = @"手机号";
    self.phoneNum.keyboardType = UIKeyboardTypeNumberPad;
    self.phoneNum.delegate = self;
    [view addSubview:self.phoneNum];
    
    self.password = [[UITextField alloc]initWithFrame:CGRectMake(60, 60, WIDTH - 90, 30)];
    self.password.placeholder = @"请填写密码";
    self.password.delegate = self;
    [view addSubview:self.password];
    
    UIButton *login = [UIButton buttonWithType:UIButtonTypeCustom];
    login.frame = CGRectMake(15, 200, WIDTH - 30, 40);
    [login addTarget:self action:@selector(doLogin) forControlEvents:UIControlEventTouchUpInside];
    [login setBackgroundImage:[UIImage imageNamed:@"register"] forState:UIControlStateNormal];
    [login setTitle:@"登录" forState:UIControlStateNormal];
    login.layer.cornerRadius = 8;
    [login setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [bgView addSubview:login];
    
    
    UIButton *regest = [UIButton buttonWithType:UIButtonTypeCustom];
    regest.frame = CGRectMake(40, 250, 40, 30);
    [regest addTarget:self action:@selector(doRegister) forControlEvents:UIControlEventTouchUpInside];
    [regest setTitle:@"注册" forState:UIControlStateNormal];
    [regest setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bgView addSubview:regest];
    
    UIButton *lostWord = [UIButton buttonWithType:UIButtonTypeCustom];
    lostWord.frame = CGRectMake(WIDTH - 120, 250, 100, 30);
    [lostWord addTarget:self action:@selector(lostWord) forControlEvents:UIControlEventTouchUpInside];
    [lostWord setTitle:@"忘记密码?" forState:UIControlStateNormal];
    [lostWord setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [bgView addSubview:lostWord];
    
    
    UILabel *wordLab = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH/2 - 150, 285, 300, 20)];
    wordLab.text = @"点击上面的“登录”按钮，即表示你同意";
    wordLab.textAlignment = NSTextAlignmentCenter;
    wordLab.font = [UIFont systemFontOfSize:13];
    wordLab.textColor = RGB(158, 158, 158);
    [bgView addSubview:wordLab];
    
    
    
    UIButton *dealBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    dealBtn.frame = CGRectMake(15, 310, WIDTH - 30, 20);
    [dealBtn addTarget:self action:@selector(readDeal) forControlEvents:UIControlEventTouchUpInside];
    [dealBtn setTitle:@"《F50season5服务协议》" forState:UIControlStateNormal];
    [dealBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    dealBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [bgView addSubview:dealBtn];
    
    
}
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
    [self createBackground];
//    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
//    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
//        NSLog(@"%ld", (long)status);
//    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    

    
}

-(void)doLogin{
    NSLog(@"login");
    // 先判断是否有输入
    if (self.phoneNum.text.length == 0 || self.password.text.length == 0) {
        // 是否需要提醒？
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"请输入手机号或者密码"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles: nil];
        [alert show];
        return;
    }
    NSString *regex = @"^((13)|(14)|(15)|(18))\\d{9}$";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    
    BOOL isMatch = [pred evaluateWithObject:self.phoneNum.text];
    
    if (!isMatch) {
        
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"请输入正确的手机号码"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil, nil];
        
        [alert show];
        
        
        return;
        
    }
    

    NSDictionary * date = [[NSDictionary  alloc]initWithObjectsAndKeys:self.phoneNum.text,@"mobile",self.password.text,@"password",@"ios",@"osType",nil];
    NSDictionary *pram = [[NSDictionary alloc]initWithObjectsAndKeys:@"member",@"class",@"login",@"func",date,@"param", nil];
    
    [[AFCustomObject shareAFCustomObject] loginActionWithParams:pram
                                                        success:^(NSDictionary *result) {
        NSLog(@"%@",result);
        NSLog(@"%@",[result objectForKey:@"errMsg"]);
        if ([[result objectForKey:@"errNo"] intValue]== 10009) {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"密码错误，请重新输入" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alertView show];
            return;
        }
        NSDictionary *dic = [result objectForKey:@"result"];
        [USER_DEFAULT setObject:[dic objectForKey:@"phone"] forKey:@"phone"];
        [USER_DEFAULT setObject:[dic objectForKey:@"token"] forKey:@"token"];
        [USER_DEFAULT setObject:[dic objectForKey:@"name"] forKey:@"name"];
        
        // 头像
        [USER_DEFAULT setObject:[dic objectForKey:@"avatar"] forKey:@"avatar"];
        // 个人简介
        [USER_DEFAULT setObject:[dic objectForKey:@"content"] forKey:@"content"];
                                                            
        
        // 关注数量
        [USER_DEFAULT setObject:[dic objectForKey:@"relations"] forKey:@"relations"];
        // 邀请数量
        [USER_DEFAULT setObject:[dic objectForKey:@"deals"] forKey:@"deals"];

        // 是否认证 1未认证 2 已认证
        [USER_DEFAULT setObject:[dic objectForKey:@"iscertificated"] forKey:@"iscertificated"];
        [USER_DEFAULT synchronize];
      
        self.tabBarController.tabBar.hidden = NO;
        [self.navigationController popToRootViewControllerAnimated:YES];
        
    } fail:^{
        NSLog(@"1111111");
    }];
}

-(void)doRegister{
    RegisterViewController *reg = [[RegisterViewController alloc]init];
    reg.titleStr = @"注册";
    [self.navigationController pushViewController:reg animated:YES];
    
}

-(void)lostWord{
    RegisterViewController *reg = [[RegisterViewController alloc]init];
    reg.titleStr = @"忘记密码";
    [self.navigationController pushViewController:reg animated:YES];
}

-(void)readDeal{
    DealViewController *deal = [[DealViewController alloc]init];
    [self.navigationController pushViewController:deal animated:YES];
}




- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField

{
    
    // When the user presses return, take focus away from the text field so that the keyboard is dismissed.

  
    [textField resignFirstResponder];
    
    return YES;       
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
